
import cmd.InvoiceJson;
import data.Invoice;
import data.InvoiceService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import json.JsonArray;

/**
 *
 * @author dakhoa
 * @version 1.0
 */
public class FilterDialogController extends AbstractController {

    private InvoiceService invoiceService = new InvoiceService();

    public void handleTableData(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String page = request.getParameter("page"); // get the requested page
//        Integer limit = Integer.valueOf(request.getParameter("rows")); // get how many rows we want to have into the grid
//        Integer sidx = Integer.valueOf(request.getParameter("sidx") == null ? "1" : request.getParameter("sidx")); // get index row - i.e. user click to sort
//        String sord = request.getParameter("sord "); // get the direction

        JsonArray ja = new JsonArray("rows");
        ja.add(genInvoices());

        response.getWriter().write(ja.toJson());
    }

    private List<InvoiceJson> genInvoices() {
        List<InvoiceJson> iwrapper = new ArrayList<InvoiceJson>();
        List<Invoice> is = invoiceService.generateInvoices();

        for (Invoice i : is) {
            iwrapper.add(new InvoiceJson(i));
        }

        return iwrapper;
    }
}
